#include <iostream>
#include "RPCCode.h"
#include <conio.h>

using namespace std;

PerfMonData g_PerfMonData;


// Memory allocation function for RPC.
// The runtime uses these two functions for allocating/deallocating
// enough memory to pass the string to the server.
void* __RPC_USER midl_user_allocate(size_t size){
    return malloc(size);
}

// Memory deallocation function for RPC.
void __RPC_USER midl_user_free(void* p){
    free(p);
}


int main(void){
	int status;
	RPCClient RPC_Client;
	int attempts(0);
	char input(0);
	status = RPC_Client.RPCConnect();
	while(attempts < 10){
		g_PerfMonData.upd_dir = 0;
		input = 0;
		Sleep(500);
		if (kbhit()) input = getch();

		if(input == 'a'){
			g_PerfMonData.upd_dir = 1;
		}
		else if(input == 's'){
			g_PerfMonData.upd_dir = -1;
		}
		RpcTryExcept{
			//g_PerfMonData.upd_dir = -1;
			RemoteCall_Update(&g_PerfMonData);
			system("cls");
			printf("Number of balls:\t%d\n",g_PerfMonData.num_balls);
			printf("Frames per sec:\t\t%6.2f\n", g_PerfMonData.cur_fps);

			for(int i(0); i<8; i++){
				printf("CPU %d:\t\t\t%3.2f ", i, g_PerfMonData.cpu_usage[i]);
				float size = g_PerfMonData.cpu_usage[i]/5.0f;
				for(int j(0); j<(int)size; j++){
					printf("#");
				}
				printf("\n");
			}
			printf("CPU Total:\t\t%3.2f ", g_PerfMonData.cpu_usage[8]);
			float size = g_PerfMonData.cpu_usage[8]/5.0f;
			for(int j(0); j<(int)size; j++){
				printf("#");
			}
			printf("\n");

			printf("Press A to add 100 balls.\n");
			printf("Press S to sub 100 balls.\n");
		}
		RpcExcept(1){
			attempts++;
			system("cls");
			printf("RPC attempt failed. Attempt: %d", attempts);
		}
		RpcEndExcept
	}
	status = RPC_Client.RPCCleanup();
	//system("pause");
	return 0;
}

